package com.ruoyi.web.controller.psi;

import com.alibaba.fastjson2.JSONArray;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.Good;
import com.ruoyi.lims.domain.LimBuildStorage;
import com.ruoyi.resp.GoodResp;
import com.ruoyi.service.GoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * lhp
 * 2023/8/23
 * 商品列表
 */
@RestController
@RequestMapping("/good")
public class GoodController extends BaseController {

    @Autowired
    private GoodService goodService;

    @Log(title = "商品数据", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('psi:category:query')")
    @RequestMapping("/list")
    public TableDataInfo list(Good good){
        startPage();
        List<GoodResp> list = goodService.getList(good);
        return getDataTable(list);
    }
    @Log(title = "增加商品", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('psi:category:add')")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody Good good){
        AjaxResult ajaxResult = goodService.add(good);
        return ajaxResult;
    }

    @Log(title = "删除商品信息", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('psi:category:del')")
    @RequestMapping("/delList")
    public AjaxResult delList(@RequestBody List<String> ids){
        AjaxResult ajaxResult = goodService.delList(ids);
        return ajaxResult;
    }
    @Log(title = "导出表格", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('psi:good:export')")
    @RequestMapping("/export")
    public void export(HttpServletResponse response, @RequestParam("list") String list) throws IOException {
        List<String> ids = JSONArray.parseArray(list, String.class);
        List<GoodResp> rows = goodService.export(ids);
        ExcelUtil<GoodResp> util = new ExcelUtil<GoodResp>(GoodResp.class);
        util.setLock(true);
        util.exportExcel(response, rows, "物料数据");
    }

    @Log(title = "导出全部表格", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('psi:good:export')")
    @RequestMapping("/exportAll")
    public void exportAll(HttpServletResponse response) throws IOException {
        List<GoodResp> rows = goodService.exportAll();
        ExcelUtil<GoodResp> util = new ExcelUtil<GoodResp>(GoodResp.class);
        util.setLock(true);
        util.exportExcel(response, rows, "物料数据");
    }

    @Log(title = "导入表格", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('psi:good:export')")
    @RequestMapping("/import")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<GoodResp> util = new ExcelUtil<GoodResp>(GoodResp.class);
        List<GoodResp> goodList = util.importExcel(file.getInputStream());
        String message = goodService.importExcel(goodList);
        return success(message);
    }

}
